package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgConsumptionDataService;
import com.csun.dsms.service.HgFinancialDataService;
import com.csun.dsms.service.HgFinancialProjectService;
import com.csun.dsms.utils.PublicUtil;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 争资争项清单
 */
@RestController
@RequestMapping("/hgFinancial")
public class HgFinancialController {
    
    @Resource
    private HgFinancialDataService hgFinancialDataService;
    @Resource
    private HgFinancialProjectService hgFinancialProjectService;

    /*
    * */
    @Authorization
    @RequestMapping(value = "/dataAdd",method = RequestMethod.POST)
    public Wrapper dataAdd (@RequestBody HgFinancialData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFinancialDataService.save(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 数据查询
     * */
    @Authorization
    @RequestMapping(value = "/dataListQuery",method = RequestMethod.POST)
    public Wrapper dataListQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgFinancialData> query = hgFinancialDataService.queryDataList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导入争资争项清单数据
     */
    @Authorization
    @RequestMapping(value = "/importFinancialData",method = RequestMethod.POST)
    public Wrapper importFinancialData (@RequestParam("file") MultipartFile file) {
        return hgFinancialDataService.importFinancialData(file);
    }

    @Authorization
    @RequestMapping(value = "/dataUpdate",method = RequestMethod.POST)
    public Wrapper dataUpdate (@RequestBody HgFinancialData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFinancialDataService.update(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }



    /*
     * */
    @Authorization
    @RequestMapping(value = "/projectAdd",method = RequestMethod.POST)
    public Wrapper projectAdd (@RequestBody HgFinancialProjectData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFinancialProjectService.save(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 数据查询
     * */
    @Authorization
    @RequestMapping(value = "/projectListQuery",method = RequestMethod.POST)
    public Wrapper projectListQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgFinancialProjectData> query = hgFinancialProjectService.queryProjectList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导入争资争项项目清单数据
     */
    @Authorization
    @RequestMapping(value = "/importFinancialProject",method = RequestMethod.POST)
    public Wrapper importFinancialProject (@RequestParam("file") MultipartFile file) {
        return hgFinancialProjectService.importFinancialProject(file);
    }

    @Authorization
    @RequestMapping(value = "/projectUpdate",method = RequestMethod.POST)
    public Wrapper projectUpdate (@RequestBody HgFinancialProjectData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFinancialProjectService.update(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }


    @Authorization
    @RequestMapping(value = "/groupByMonth",method = RequestMethod.POST)
    public Wrapper groupByMonth (@RequestBody HgQueryDto hgQueryDto){
        List<HgYearMonthVo> query = hgFinancialDataService.groupByMonth(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导出争资争项清单数据模板
     */
    @GetMapping("/downFinancialTemplate")
    public void downFinancialTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "争资争项清单数据导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("行业名称"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("中央省预算金额"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("中央省预算目标"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("两重两新超长期特别国债金额"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("两重两新超长期特别国债目标"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("本地债券金额"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("本地债券目标"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("其他基金金额"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("其他基金目标"));
        sheet.setColumnWidth(12, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(13);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("总基金金额"));
        sheet.setColumnWidth(13, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(14);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("总基金目标"));
        sheet.setColumnWidth(14, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(15);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(15, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(16);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(16, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(17);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(17, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(18);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(18, 20 * 256);

        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出争资争项项目清单数据模板
     */
    @GetMapping("/downFinancialProjectTemplate")
    public void downFinancialProjectTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "争资争项项目清单数据导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("行业名称"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("项目名称"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("项目基金"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("应用程序批处理名称"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("建议关注"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(12, 20 * 256);

        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
